package com.edu.policy.utils;
import okhttp3.*;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class OkhttpRequestUtil {
    private static final OkHttpClient client;

    // 初始化 OkHttpClient，设置超时等配置
    static {
        client = new OkHttpClient.Builder()
                .connectTimeout(100, TimeUnit.SECONDS)
                .readTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .build();
    }

    /**
     * 发送 GET 请求
     *
     * @param url 请求地址
     * @param headers 请求头（可选）
     * @return 响应体字符串
     * @throws IOException
     */
    public static String sendGet(String url, Map<String, String> headers) throws IOException {
        Request.Builder requestBuilder = new Request.Builder().url(url);

        // 添加请求头
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        Request request = requestBuilder.build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            return response.body().string();
        }
    }

    /**
     * 发送 POST 请求
     *
     * @param url 请求地址
     * @param jsonBody 请求体（JSON 格式字符串）
     * @param headers 请求头（可选）
     * @return 响应体字符串
     * @throws IOException
     */
    public static String sendPost(String url, String jsonBody, Map<String, String> headers) throws IOException {
        RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json; charset=utf-8"));

        Request.Builder requestBuilder = new Request.Builder().url(url).post(body);

        // 添加请求头
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        Request request = requestBuilder.build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            return response.body().string();
        }
    }

    /**
     * 发送带 form 表单的 POST 请求
     *
     * @param url 请求地址
     * @param formParams 表单参数
     * @param headers 请求头（可选）
     * @return 响应体字符串
     * @throws IOException
     */
    public static String sendPostForm(String url, Map<String, String> formParams, Map<String, String> headers) throws IOException {
        FormBody.Builder formBuilder = new FormBody.Builder();

        // 添加表单参数
        if (formParams != null) {
            for (Map.Entry<String, String> entry : formParams.entrySet()) {
                formBuilder.add(entry.getKey(), entry.getValue());
            }
        }

        RequestBody formBody = formBuilder.build();
        Request.Builder requestBuilder = new Request.Builder().url(url).post(formBody);

        // 添加请求头
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        Request request = requestBuilder.build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            return response.body().string();
        }
    }
}
